import java.util.Arrays;

/**
 * 1833. 雪糕的最大数量
 * https://leetcode-cn.com/problems/maximum-ice-cream-bars/
 */
public class Solutions_1833 {
    public static void main(String[] args) {
//        int[] costs = {1, 3, 2, 4, 1};
//        int coins = 7;  // output: 4

//        int[] costs = {10, 6, 8, 7, 7, 8};
//        int coins = 5;  // output: 0

        int[] costs = {1, 6, 3, 1, 2, 5};
        int coins = 20;  // output: 6

        int result = maxIceCream(costs, coins);
        System.out.println(result);
    }

    public static int maxIceCream(int[] costs, int coins) {
        Arrays.sort(costs);
        int res = 0;
        for (int cost : costs) {
            if (coins >= cost) {
                coins -= cost;
                res ++;
            } else {
                // 提前结束循环，因为 coins 的值都将小于后面的元素
                break;
            }
        }
        return res;
    }
}
